
package com.insigma.business.qrcode.soldier.service.impl;


import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.soldier.dao.VenueAppointmentScheduleDao;
import com.insigma.business.qrcode.soldier.dao.VenueAppointmentStatisticsDao;
import com.insigma.business.qrcode.soldier.entity.VenueAppointmentScheduleDO;
import com.insigma.business.qrcode.soldier.entity.VenueAppointmentStatisticsDO;
import com.insigma.business.qrcode.soldier.request.VenueAppointmentStatisticsBSearcher;
import com.insigma.business.qrcode.soldier.service.VenueAppointmentStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service("venueAppointmentStatisticsService")
public class VenueAppointmentStatisticsServiceImpl implements VenueAppointmentStatisticsService {

    @Autowired(required = false)
    private VenueAppointmentStatisticsDao venueAppointmentStatisticsDao;

    @Autowired(required = false)
    private VenueAppointmentScheduleDao venueAppointmentScheduleDao;

    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    
    @Override
    public List<VenueAppointmentStatisticsDO> search(final VenueAppointmentStatisticsBSearcher venueAppointmentStatisticsBSearcher) {
        if (venueAppointmentStatisticsBSearcher.getVenueIds() != null && venueAppointmentStatisticsBSearcher.getVenueIds().size() == 0) {
            venueAppointmentStatisticsBSearcher.setTotalCount(0);
            return new ArrayList<>();
        }
        return venueAppointmentStatisticsDao.search(venueAppointmentStatisticsBSearcher);
    }
   
    @Override
    public VenueAppointmentStatisticsDO get(final String id){
        return venueAppointmentStatisticsDao.get(id);
    }

    
    @Override
    public Integer insert(final VenueAppointmentStatisticsDO venueAppointmentStatisticsDO){
        venueAppointmentStatisticsDO.setId(sequenceGenerator.genKey("VENUE_APPOINTMENT_STATISTICS_ID"));
        return venueAppointmentStatisticsDao.insert(venueAppointmentStatisticsDO);
    }

    
    @Override
    public Integer update(final VenueAppointmentStatisticsDO venueAppointmentStatisticsDO){
        return venueAppointmentStatisticsDao.update(venueAppointmentStatisticsDO);
    }

    
    @Override
    public Integer delete(final String id, final String modifierType, final String modifierId){
        return venueAppointmentStatisticsDao.delete(id,modifierType,modifierId);
    }

    @Override
    public VenueAppointmentStatisticsDO getByAppointScheduleId(String id, String appointDate) {
        return venueAppointmentStatisticsDao.getByAppointScheduleId(id, appointDate);
    }

    @Override
    public String addUsed(String appointDate, String appointTimeId, Integer appointNum, Boolean notLimited) {
        VenueAppointmentStatisticsDO statisticsDO = venueAppointmentStatisticsDao.getByAppointScheduleId(appointTimeId, appointDate);
        if (statisticsDO == null) {
            VenueAppointmentScheduleDO venueAppointmentScheduleDO = venueAppointmentScheduleDao.get(appointTimeId);
            statisticsDO = new VenueAppointmentStatisticsDO();
            statisticsDO.setAppointDate(appointDate);
            statisticsDO.setAppointTimeId(appointTimeId);
            statisticsDO.setMaxAppointmentNum(venueAppointmentScheduleDO.getMaxAppointmentNum());
            statisticsDO.setRemainingNum(venueAppointmentScheduleDO.getMaxAppointmentNum());
            statisticsDO.setVenueId(venueAppointmentScheduleDO.getVenueId());
            insert(statisticsDO);
        }
        BizCheckUtil.checkIsTrue(notLimited || statisticsDO.getRemainingNum() >= appointNum, "名额不足");

        while (true) {
            int i = venueAppointmentStatisticsDao.addUsed(statisticsDO.getId(), statisticsDO.getRemainingNum(), appointNum);
            if (i > 0) {
                return statisticsDO.getId();
            }
            statisticsDO = venueAppointmentStatisticsDao.get(statisticsDO.getId());
            BizCheckUtil.checkIsTrue(notLimited || statisticsDO.getRemainingNum() >= appointNum, "名额不足");
        }
    }

    @Override
    public String releaseUsed(String id, Integer appointNum) {
        VenueAppointmentStatisticsDO statisticsDO = venueAppointmentStatisticsDao.get(id);
        while (true) {
            int i = venueAppointmentStatisticsDao.releaseUsed(statisticsDO.getId(), statisticsDO.getRemainingNum(), appointNum);
            if (i > 0) {
                return statisticsDO.getId();
            }
            statisticsDO = venueAppointmentStatisticsDao.get(statisticsDO.getId());
        }
    }

}
